const fs   = require("fs")
const path = require("path")
const configPath = "../script/config/"
const config = require(configPath+"action.config")

// let actionMap = {}

const mapDirs = (dirname,fnArr)=>{
	console.log(">>"+dirname)
	let fns = fs.readdirSync(dirname);
	fns.forEach(fn=>{
		let nfn = path.join(dirname,fn)
		let stat = fs.statSync(nfn);
		if(stat.isDirectory())
			mapDirs(nfn,fnArr)
		else if(stat.isFile()){
			let rfn = filter(fn)
			if(rfn)
				fnArr.push(rfn)
			// actionMap[rfn]=nfn
			// if(rfn)
				// fs.renameSync(nfn,path.join(dirname,rfn+".action.js"))
		}
	})
}
const filter = (fn)=>{
	// path-remove-mng.js
	let reg = /^.*?(?=[\.\-]action\.js)/
	let r = fn.match(reg)
	let c1 = !fn.match(/(base|abstract)/)
	if(r&&c1){
		let rn = r[0].split(/\-/).map(n=>{
			return n.replace(/^[\w]/,(a)=>a.toUpperCase())
		}).join("")
		return rn
	}else
	console.log("***********",fn)
}
// console.log(config)
let dirname = path.join(__dirname,config.baseDir)
let fnArr = []
let fnMap = []

mapDirs(dirname,fnArr)

fnArr.forEach(fn=>{
	let k = fn//.replace(/^[\w]/,(a)=>a.toLowerCase())
	if(fnMap[k])throw new Error("Action map key dupclicated")
	else fnMap[k] = fn
})

fnMap = Object.assign(fnMap,config.default)
console.log(fnMap,fnArr.length)
let format = []
for(let k in fnMap)
	format.push(`\n\t${k}:'${k}'`)
let code = `const Action = {${format.join()}\n}\nexport default Action`
// code = code.replace(/\,/g,",\n\t")
let actionPath = path.join(__dirname,configPath+"/action.js")
// console.log(actionMap)
if(fs.existsSync(actionPath))
	fs.unlinkSync(actionPath)
fs.writeFileSync(actionPath,code)


